/*
 * Copyright 2012 Biz-e (info@biz-e.nl)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package nl.biz_e.android.calllog.dao;

import static android.provider.CallLog.Calls.*;

import java.util.Date;
import java.util.Vector;

import nl.biz_e.android.calllog.CallLogApp;
import nl.biz_e.android.calllog.model.CallLogItem;
import android.content.Context;
import android.database.Cursor;
import android.provider.CallLog;
import android.util.Log;

/**
 * TODO: Write type comment
 * 
 * @author Joost A. Bloemsma
 */
public class LogDao {

	private static final String DEBUG_TAG = LogDao.class.getSimpleName();
	private Context _ctx;
	private static LogDao _instance = null;
	
	private LogDao(Context ctx) {
		super();
		this._ctx = ctx;
	}
	
	/**
	 * Factory method. Makes sure this is a singleton.
	 * @return Instance of this DAO
	 */
	public static LogDao getInstance() {
		if (_instance == null) {
			_instance = new LogDao(CallLogApp.getAppContext());
		}
		return _instance;
	}
	
	/**
	 * Returns a {@link Vector} of {@link CallLogItem}s representing <b>all</b> entries in the phone log.
	 * @return {@link Vector}<{@link CallLogItem}>
	 */
	public Vector<CallLogItem> getLogItems() {
		return this.getLogItems(false);
	}
	
	/**
	 * Returns a {@link Vector} of {@link CallLogItem}s representing the entries in the phone log.
	 * @param newOnly if {@code true}, only those items will be returned that are not yet in the database. 
	 * Otherwise, all Items will be returned.
	 * @return {@link Vector}<{@link CallLogItem}>
	 */
	public Vector<CallLogItem> getLogItems(boolean newOnly) {
		Vector<CallLogItem> items = new Vector<CallLogItem>();
		Cursor cur = _ctx.getContentResolver().query(CallLog.Calls.CONTENT_URI, null, null, null, null);
		
    	if (cur.moveToLast()) {
    		String name;
    		String number;
    		long datestart;
    		int duration;
    		
    		int cachedNameCol = cur.getColumnIndex(CACHED_NAME);
    		int numberCol = cur.getColumnIndex(NUMBER);
    		int startCol = cur.getColumnIndex(DATE);
    		int durCol =  cur.getColumnIndex(DURATION);
    		
    		int cachedNumberLabelCol = cur.getColumnIndex(CACHED_NUMBER_LABEL);
    		int cachedNumberTypeCol = cur.getColumnIndex(CACHED_NUMBER_TYPE);
    		int typeCol = cur.getColumnIndex(TYPE);
    		
    		do {
    			name = cur.getString(cachedNameCol);
    			number = cur.getString(numberCol);
    			datestart = cur.getLong(startCol);
    			duration = cur.getInt(durCol);
    			
    			CallLogItem itm = new CallLogItem(name, number, new Date(datestart), duration);
    			if (!newOnly || !LogDBDao.getInstance().isLoggedItem(itm)) {
    				itm.setCachedNumberLabel(cur.getString(cachedNumberLabelCol));
    				itm.setCachedNumberType(cur.getInt(cachedNumberTypeCol));
    				itm.setType(cur.getInt(typeCol));
    				ContactDao.getInstance().addContactData(itm);
    				items.add(itm);
    			}
    		} while (cur.moveToPrevious());
    	}
    	cur.close();
    	if (Log.isLoggable(DEBUG_TAG, Log.DEBUG)) Log.d(DEBUG_TAG, "Retrieved " + items.size() + " items");
		return items;
	}
	
}

